﻿Public Class OPERATETEMPLATE
    Dim connect As CONDB = CONDB.NewConnection()
    Dim s As FILTERCLASS
    Dim Sql As String
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub OPERATETEMPLATE_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridmasclsoperat()
        SelectDatagridClinic()
        SearchCoperate()

        DesignForm(Me)
        DesignGrid(MASCLINICGrid)
        DesignGrid(MASCLSOPERATEGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub
    Private Sub SelectDatagridmasclsoperat()
        Dim objSelectDataMasclsoperate As New MASOPERATE
        Dim MasclsOperatData As DataTable
        MasclsOperatData = connect.GetTable(objSelectDataMasclsoperate.selectmasclsoperate_)
        MASCLSOPERATEGrid.PrimaryGrid.DataSource = MasclsOperatData
    End Sub
    Private Sub SelectDatagridClinic()
        Dim objSelectDataClinic As New MASOPERATE
        Dim MASCLINIC As DataTable
        If Convert.ToString(COPERATENAMETxt.Tag).Trim = "" Then
            MASCLINIC = connect.GetTable(objSelectDataClinic.SelectMasclinic_)
        Else
            MASCLINIC = connect.GetTable(objSelectDataClinic.SelectMasclinic2_(COPERATENAMETxt.Tag))
        End If
        MASCLINICGrid.PrimaryGrid.DataSource = MASCLINIC
    End Sub

    Private Sub COPERATENAMETxt_Click(sender As Object, e As EventArgs) Handles COPERATENAMETxt.Click
        COPERATENAMETxt.Text = ""
        COPERATENAMETxt.Tag = ""
    End Sub
   
    Private Sub cleardataMasclsoperate()
        COPERATENAMETxt.Text = ""
        CODEOPERATETxt.Text = ""
        'searchclinic.Text = ""
        masclsoperateStatus.Checked = False
    End Sub
    Private Sub SearchCoperate()
        Dim sql2 As String
        sql2 = "SELECT `coperateid`,`coperatename`,`code` FROM masclsoperate WHERE `status` = 1 "
        s = New FILTERCLASS(COPERATENAMETxt, sql2, "ID หมวดหัตถการ,ชื่อหมวดหัตถการ,รหัสหมวดหัตถการ", "80,100,120", "0,1,1", "0,1,1")
        's.SetTagIndex = 0 ' settag
        's.SetTextIndex = 1 'settext
        s.SetShowBorder = True
    End Sub
    Private Sub MASCLINICGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASCLINICGrid.CellClick
        connect = CONDB.NewConnection()
        connect.BeginTrans()
        Try
            If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value()) = True Then
                Sql = "UPDATE masclinic SET  `coperateid` = '" & Convert.ToString(COPERATENAMETxt.Tag).Trim & "' WHERE `clinic` = '" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value & "'; "
            Else
                Sql += "UPDATE masclinic SET  `coperateid` = Null WHERE `clinic` = '" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value & "'; "
            End If
            connect.ExecuteNonQuery(Sql)
            connect.CommitTrans()
        Catch ex As Exception
            connect.RollbackTrans()
            MsgBox(ex.Message)
        Finally
            connect.Dispose()
        End Try
    End Sub

    Private Sub MASCLSOPERATEGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASCLSOPERATEGrid.CellClick
        Dim Sql As String
        connect = CONDB.NewConnection()
        connect.BeginTrans()
        Try
            If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value()) = True Then
                Sql = "INSERT INTO masoperateincls (`coperateid`,`operatid`) VALUES ('" & Convert.ToString(COPERATENAMETxt.Tag).Trim & "','" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสหัตถการ").Value & "') "
            Else
                Sql += "DELETE FROM masoperateincls WHERE (coperateid = '" & COPERATENAMETxt.Tag & "' and operatid = '" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสหัตถการ").Value & "') "
            End If
            connect.ExecuteNonQuery(Sql)
            connect.CommitTrans()
        Catch ex As Exception
            connect.RollbackTrans()
            MsgBox(ex.Message)
        Finally
            connect.Dispose()
        End Try
    End Sub

    Private Sub CODEOPERATETxt_KeyPress(sender As Object, e As KeyPressEventArgs) Handles CODEOPERATETxt.KeyPress
        If CODEOPERATETxt.Text.Length > 3 And Not Char.IsControl(e.KeyChar) Then
            e.KeyChar = ""
        End If
    End Sub

    Private Sub COPERATENAMETxt_TextChanged(sender As Object, e As EventArgs) Handles COPERATENAMETxt.TextChanged
        SelectDatagridClinic()

        connect = CONDB.NewConnection
        Dim sql As String
        Dim dt As New DataTable
        sql = "SELECT `code`,masclinic.`clinicname`,masclsoperate.`status`,masclinic.`clinic` FROM masclsoperate LEFT JOIN (SELECT * FROM masclinic WHERE `status` = 1) as masclinic ON masclsoperate.clinic = masclinic.clinic WHERE masclsoperate.`coperateid` = '" & Convert.ToString(COPERATENAMETxt.Tag).Trim & "';"
        dt = connect.GetTable(sql)
        If dt.Rows.Count > 0 Then
            CODEOPERATETxt.Text = dt.Rows(0)("CODE").ToString.Trim
            'searchclinic.Text = dt.Rows(0)("CLINICNAME").ToString.Trim
            masclsoperateStatus.Checked = dt.Rows(0)("STATUS")
            'clinictag.Text = dt.Rows(0)("CLINIC").ToString.Trim
        End If
        sql = "SELECT x.* FROM (SELECT `coperateid`,operatitem.`operatid` as 'รหัสหัตถการ',operatitem.`operatename_th` as 'ชื่อหัตถการภาษาไทย',operatitem.`operatename_en` as 'ชื่อหัตถการภาษาอังกฤษ',operatitem.`code` AS 'CODE',masoperateincls.`status` as 'เลือก' FROM masoperateincls LEFT JOIN (SELECT * FROM operatitem WHERE `status` = 1)  as operatitem ON masoperateincls.operatid = operatitem.operatid WHERE masoperateincls.`coperateid` = '" & Convert.ToString(COPERATENAMETxt.Tag).Trim & "' UNION SELECT '' AS `coperateid`,operatitem.`operatid` as 'รหัสหัตถการ',operatitem.`operatename_th` as 'ชื่อหัตถการภาษาไทย',operatitem.`operatename_en` as 'ชื่อหัตถการภาษาอังกฤษ',operatitem.`code` AS 'CODE',0 as 'เลือก' FROM operatitem   WHERE  `status` = 1) AS x GROUP BY `รหัสหัตถการ`;"
        MASCLSOPERATEGrid.PrimaryGrid.DataSource = connect.GetTable(sql)
        MASCLINICGrid.PrimaryGrid.SetSort(MASCLINICGrid.PrimaryGrid.Columns(3), DevComponents.DotNetBar.SuperGrid.SortDirection.Descending)
        MASCLSOPERATEGrid.PrimaryGrid.SetSort(MASCLSOPERATEGrid.PrimaryGrid.Columns(6), DevComponents.DotNetBar.SuperGrid.SortDirection.Descending)
    End Sub

    Private Sub addMasclsoperate_Click_1(sender As Object, e As EventArgs) Handles addMasclsoperate.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If COPERATENAMETxt.Text.ToString.Length > 0 And CODEOPERATETxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New MASOPERATE
                    'If Convert.ToString(searchclinic.Tag).Trim <> "" Then
                    '    objInsertData.CLINIC_ = Convert.ToString(searchclinic.Tag).Trim
                    'End If
                    objInsertData.COPERATENAME_ = COPERATENAMETxt.Text
                    objInsertData.CODEOPERATE_ = CODEOPERATETxt.Text
                    'objInsertData.CLINIC_ = Convert.ToString(searchclinic.Tag).Trim
                    objInsertData.STATUS_ = masclsoperateStatus.Checked

                    objInsertData.insertMasclsoperate()

                    SelectDatagridmasclsoperat()

                    cleardataMasclsoperate()
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
                'SearchClinicboy()
                SearchCoperate()
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editMasclsoperate_Click_1(sender As Object, e As EventArgs) Handles editMasclsoperate.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If COPERATENAMETxt.Text.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
                    If editStat = 1 Then
                        Dim objeditData As New MASOPERATE
                        objeditData.COPERATEID_ = Convert.ToString(COPERATENAMETxt.Tag).Trim
                        objeditData.COPERATENAME_ = COPERATENAMETxt.Text
                        objeditData.CODEOPERATE_ = CODEOPERATETxt.Text
                        'objeditData.CLINIC_ = Convert.ToString(searchclinic.Tag)
                        objeditData.STATUS_ = masclsoperateStatus.Checked
                        objeditData.editMasclsoperate()

                        SelectDatagridmasclsoperat()

                        cleardataMasclsoperate()

                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
                s.SetSql = "SELECT `coperateid`,`coperatename`,`code` FROM masclsoperate WHERE `status` = 1 "
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub
End Class